//
//  JoinHomeViewController.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/22.
//  Copyright © 2020 tym. All rights reserved.
//

#import "JoinHomeViewController.h"
#import "WCQRCodeVC.h"
#import "FamilyService.h"
#import "IQKeyboardManager.h"
#import "FamilyModel.h"
#import "JosinFamilyVC.h"
#import "TYMBaseTableView.h"
#import "JoinHomeViewCell.h"
@interface JoinHomeViewController ()


@property(nonatomic,weak) IBOutlet UIView  *conView;

@property(nonatomic,weak) IBOutlet UITextField *searchFid;
@property(nonatomic,weak) IBOutlet TYMBaseTableView *tableView;

@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,strong) FamilyService *service;
@end

@implementation JoinHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"加入已有家庭"];
    [self addRightBtnImage:@"icon_nav_scam"];
     [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
    [self initUI];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}


-(void)initUI{
    self.conView.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
    self.conView.layer.cornerRadius = 5;
    self.conView.layer.shadowColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:0.5].CGColor;
    self.conView.layer.shadowOffset = CGSizeMake(0,0);
    self.conView.layer.shadowOpacity = 1;
    self.conView.layer.shadowRadius = 14;
    
    [self.tableView registerNib:[UINib nibWithNibName:[JoinHomeViewCell identifier ] bundle:nil] forCellReuseIdentifier:[JoinHomeViewCell identifier]];
    self.tableView.backgroundColor = viewBGColor;
}

-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

-(void)rightFunc:(id)sender{
    
    WCQRCodeVC *wcqVC = [[WCQRCodeVC alloc] init];
    [self.navigationController pushViewController:wcqVC animated:YES];
}

-(IBAction)searchAction:(id)sender{
    TYMWeak(self);
    [self.service getFindFamilysWithName:self.searchFid.text page:1 size:999 completion:^(NSError * _Nonnull error, NSArray<FamilyModel *> * _Nonnull array) {
        if(weakself.dataArray.count){
            [weakself.dataArray removeAllObjects];
        }
        [weakself.dataArray addObjectsFromArray:array];
        [weakself.tableView reloadData];
    }];
}

-(FamilyService*)service{
    if (_service == nil) {
        _service = [[FamilyService alloc] init];
    }
    return _service;
}

#pragma  mark - tableView 代理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [JoinHomeViewCell cellForHeight];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JoinHomeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[JoinHomeViewCell identifier] forIndexPath:indexPath];
    if (self.dataArray.count) {
        cell.model = self.dataArray[indexPath.row];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
  
    JosinFamilyVC *joinFamily = [[JosinFamilyVC alloc] init];
    joinFamily.familyModel = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:joinFamily animated:YES];
}


@end
